Page({
    data: {
        product: {},
        comments: [],
        newComment: '',
        userid: ''
    },

    onLoad: function () {
        // 获取存储的商品信息
        const product = wx.getStorageSync('currentPrdt');
        this.setData({
            product
        });
        this.setData({
            userid: wx.getStorageSync('userID')
        });

        // 获取已有评价
        this.fetchComments(product.productID); // 假设 product.id 是商品的 ID
    },

 

    fetchComments: function (productID) {
        // 假设您有一个 API 来获取商品的评价
        wx.request({
            url: 'http://192.168.91.86:8080/comments/' + productID,
            method: 'GET',
            success: (res) => {
                console.log(res.data)
                if (res.data.code == 1) {
                    this.setData({
                        comments: res.data.data
                    });
                }
            },
            fail: (err) => {
                console.error(err);
            }
        });
    },

    onInput: function (e) {
        this.setData({
            newComment: e.detail.value
        });
    },

    delete: function (e) {
        let commentid = e.currentTarget.dataset.comment.commentID;
        let productID = wx.getStorageSync('currentPrdt').productID
        console.log(commentid);
        wx.request({
            url: 'http://192.168.91.86:8080/comments/'+commentid, // 接口地址
            method: 'DELETE', // 请求方法
            success: (res) =>{
                if (res.data.code == 1) {
                    // 删除成功后的处理逻辑
                    wx.showToast({
                        title: '删除成功',
                        icon: 'success'
                    });
                    this.fetchComments(productID)
                }
            },
            fail: function () {
                // 请求失败的处理
                wx.showToast({
                    title: '请求失败',
                    icon: 'none'
                });
            }
        });
    },
  
    submitComment: function () {
        const {
            newComment,
            product
        } = this.data;
        const userID = wx.getStorageSync("userID"); 

        if (!newComment) {
            wx.showToast({
                title: '评价内容不能为空',
                icon: 'none'
            });
            return;
        }

        wx.request({
            url: 'http://192.168.91.86:8080/comments',
            method: 'POST',
            data: {
                userID: userID,
                productID: product.productID, 
                content: newComment
            },
            success: (res) => {
                if (res.data.code === 1) {
                    wx.showToast({
                        title: '评价提交成功',
                        icon: 'success'
                    });
                    this.setData({
                        newComment: ''
                    }); // 清空输入框
                    this.fetchComments(product.productID); // 刷新已有评价
                } else {
                    wx.showToast({
                        title: '提交失败，请重试',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                console.error(err);
                wx.showToast({
                    title: '网络错误，请重试',
                    icon: 'none'
                });
            }
        });
    }
});